﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;

namespace common.forms
{
    public partial class getImage : baseImageProcess
    {
        public getImage()
        {
            InitializeComponent();
        }
        public override void SetLanguage()
        {
            base.SetLanguage();
            this.getImageBtn.Text = language.GetString("takePicture");
            this.clearBtn.Text = language.GetString("clear");
        }
        protected override void GetImage()
        {
            WIALib.WiaClass wiaManager = null;		// WIA manager COM object
            WIALib.CollectionClass wiaDevs = null;		// WIA devices collection COM object
            WIALib.ItemClass wiaRoot = null;		// WIA root device COM object
            WIALib.CollectionClass wiaPics = null;		// WIA collection COM object
            WIALib.ItemClass wiaItem = null;		// WIA image COM object
            try
            {
                wiaManager = new WIALib.WiaClass();		// create COM instance of WIA manager

                wiaDevs = wiaManager.Devices as WIALib.CollectionClass;			// call Wia.Devices to get all devices
                if ((wiaDevs == null) || (wiaDevs.Count == 0))
                {
                    SysLibs.ShowErrorMessage("No WIA devices found!");
                    return;
                }
                object selectUsingUI = System.Reflection.Missing.Value;			// = Nothing
                wiaRoot = (WIALib.ItemClass)wiaManager.Create(ref selectUsingUI);	// let user select device
                if (wiaRoot == null)											// nothing to do
                    return; 

                // this call shows the common WIA dialog to let the user select a picture:
                wiaPics = wiaRoot.GetItemsFromUI(WIALib.WiaFlag.SingleImage, WIALib.WiaIntent.ImageTypeColor) as WIALib.CollectionClass;
                if (wiaPics == null) return;

                string imageFileName = null;
                this.ShowCurrorWait();                      // could take some time
                bool takeFirst = true;						// this sample uses only one single picture
                foreach (object wiaObj in wiaPics)			// enumerate all the pictures the user selected
                {
                    if (takeFirst)
                    {
                        DisposeImage();						// remove previous picture
                        wiaItem = (WIALib.ItemClass)Marshal.CreateWrapperOfType(wiaObj, typeof(WIALib.ItemClass));

                        RemoveTempFile();
                        imageFileName = GetTempFile("");				// create temporary file for image
                        this.Refresh();
                        wiaItem.Transfer(imageFileName, false);			// transfer picture to our temporary file
                        emImage.Image = Image.FromFile(imageFileName);	// create Image instance from file
                        takeFirst = false;									// first and only one done.
                    }
                    Marshal.ReleaseComObject(wiaObj);					// release enumerated COM object
                }
                this.myResultFileName = imageFileName;
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
            finally
            {
                if (wiaItem != null) Marshal.ReleaseComObject(wiaItem);		// release WIA image COM object
                if (wiaPics != null) Marshal.ReleaseComObject(wiaPics);		// release WIA collection COM object
                if (wiaRoot != null) Marshal.ReleaseComObject(wiaRoot);		// release WIA root device COM object
                if (wiaDevs != null) Marshal.ReleaseComObject(wiaDevs);		// release WIA devices collection COM object
                if (wiaManager != null) Marshal.ReleaseComObject(wiaManager);		// release WIA manager COM object
                this.ShowCurrorDefault();				// restore cursor
            }
            return;
        }
        private void selectBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void clearBtn_Click(object sender, EventArgs e)
        {
            try
            {
                myImage = null;
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
    }
}
